<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false" width="71%" :visible.sync="visible">
    <el-form :model="dataForm" :inline="true" :rules="dataRule" ref="dataForm" label-width="120px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="客户姓名" prop="customName">
        <el-input v-model="dataForm.customName" :disabled="disabled" placeholder="客户姓名" clearable/>
      </el-form-item>
      <el-form-item label="客户手机号" prop="customPhone">
        <el-input v-model="dataForm.customPhone" :disabled="disabled" placeholder="客户手机号" clearable/>
      </el-form-item>
      <el-form-item label="科室" prop="departmentCode">
        <el-dict code="department" v-model="dataForm.departmentCode"></el-dict>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="dataForm.userName" :disabled="disabled" placeholder="用户名" clearable/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="dataForm.status" placeholder="状态" clearable>
          <el-option value="init" label="未处理"></el-option>
          <el-option value="claimed" label="已认领"></el-option>
          <el-option value="processed" label="已处理"></el-option>
          <el-option value="cancel" label="已取消"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-select v-model="dataForm.source" placeholder="来源" clearable>
          <el-option value="wx_oa" label="微信公众号"></el-option>
          <el-option value="wb" label="微博"></el-option>
          <el-option value="baidu" label="百度"></el-option>
          <el-option value="douyin" label="抖音"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二級来源" prop="secondarySource">
        <el-select v-model="dataForm.secondarySource" placeholder="二級来源" clearable>
          <el-option value="mldyy" label="美琳达医院"></el-option>
          <el-option value="mldfkzx" label="美琳达妇科中心"></el-option>
          <el-option value="mldyz" label="美琳达月子"></el-option>
          <el-option value="mldfwzx" label="美琳达服务中心"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        dataForm: {
          id: '',
          createTime: '',
          updateTime: '',
          customName: '',
          customPhone: '',
          departmentCode: '',
          userName: '',
          status: '',
          source: '',
          secondarySource: ''},
        dataRule: {
          customName: [{required: true, message: '客户姓名不能为空', trigger: 'blur'}],
          customPhone: [{required: true, message: '电话不能为空', trigger: 'blur'}, {pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机格式', trigger: ['blur']}],
          departmentCode: [{required: true, message: '科室不能为空', trigger: 'blur'}],
          status: [{required: true, message: '状态不能为空', trigger: 'blur'}],
          source: [{required: true, message: '来源不能为空', trigger: 'blur'}],
          other: []
        }
      }
    },
    methods: {
      init (id, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/mld/customreservation/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.info
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/mld/customreservation/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({ message: '操作成功', type: 'success', duration: 1500 })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      }
    }
  }
</script>
